package com.st.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.st.mall.product.entity.AttrEntity;
import com.st.mall.product.vo.AttrGroupEntityVo;
import com.st.mall.product.vo.AttrGroupResVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.st.mall.product.entity.AttrGroupEntity;
import com.st.mall.product.service.AttrGroupService;
import com.st.common.utils.PageUtils;
import com.st.common.utils.R;



/**
 * 属性分组
 *
 * @author fg
 * @email f@163.com
 * @date 2023-02-26 20:46:23
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupResVo> list= attrGroupService.getWithAttr(catelogId);
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @GetMapping("/list/{cateLogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("cateLogId") Long cateLogId){
        PageUtils page = attrGroupService.queryPage(params,cateLogId);
        return R.ok().put("page", page);
    }
    /**
     * 获取指定分组关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getGroupRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> page = attrGroupService.getRelation(attrgroupId);
        return R.ok().put("data", page);
    }
    /**
     * 获取指定分组没有关联的所有属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttr(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrGroupService.getNoAttr(params,attrgroupId);
        return R.ok().put("data", page);
    }

    /**
     * 获取分类完整路径
     */
    @RequestMapping("/info/{attrGroupId}")

    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCateLogPath(attrGroupService.getCtLogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupEntityVo> vos){
        attrGroupService.saveRelation(vos);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    /**
     * 删除属性与分组的关联关系
     */
    @DeleteMapping("/attr/relation/delete")

    public R deleteRelation(@RequestBody AttrGroupEntityVo vo){
        attrGroupService.deleteRelation(vo);

        return R.ok();
    }

}
